package com.example.zhihuhelpapp.mvp;

import android.content.Context;
import android.text.TextUtils;

import com.example.zhihuhelpapp.api.mvp.RxPresenter;
import com.example.zhihuhelpapp.mvp.contract.IMainContract;
import com.example.zhihuhelpapp.utils.CoreConstant;
import com.example.zhihuhelpapp.utils.CoreLib;
import com.example.zhihuhelpapp.utils.DialogUtils;
import com.example.zhihuhelpapp.utils.SpUtils;
import com.example.zhihuhelpapp.utils.UIUtils;

public class MainPresenter extends RxPresenter<IMainContract.IView> implements IMainContract.IPresenter {
    @Override
    public void checkPageContent(Context context) {
        String currentPageContent = UIUtils.getPastContent(CoreLib.getContext());
        String lastCopyUrl = SpUtils.getInstance().get(CoreConstant.SpConstant.KEY_LAST_COPY_URL, "");
        if (TextUtils.equals(currentPageContent, lastCopyUrl)) {
            return;
        }
        if (CoreLib.isAnswer(currentPageContent)) {
            String answerId = CoreLib.getQuestionOrAnswerId(currentPageContent);
            SpUtils.getInstance().put(CoreConstant.SpConstant.KEY_LAST_COPY_URL, currentPageContent);
            DialogUtils.showDialog(context, "提示", "检测到回答：" + currentPageContent + "是否添加？",
                    (dialog, which) -> mView.onNewAnswer(answerId));
        }else if(CoreLib.isQuestion(currentPageContent)){
            String questionId = CoreLib.getQuestionOrAnswerId(currentPageContent);
            SpUtils.getInstance().put(CoreConstant.SpConstant.KEY_LAST_COPY_URL, currentPageContent);
            DialogUtils.showDialog(context, "提示", "检测到问题：" + currentPageContent + "是否添加？",
                    (dialog, which) -> mView.onNewQuestion(questionId));
        }
    }
}
